/**
 * 
 */
package com.mvc.controller.healthmanager;

import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.huangqj.consant.ValidatorUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.mvc.controller.base.BaseController;
import com.mvc.model.base.Json;
import com.mvc.model.healthmanager.archive.BloodOxygen;
import com.mvc.model.healthmanager.archive.BloodPressure;
import com.mvc.model.mb.archive.BaseInfo;
import com.mvc.service.healthmanager.BloodOxygenServiceI;

/**
 * 血氧接口控制器
 * @author qingjie
 *
 */
@Controller
@RequestMapping("/oxygen")
public class bloodOxygenController extends BaseController {

	@Autowired
	private BloodOxygenServiceI bOxygenServiceI;
	
	/**
	 * 血氧对外接口
	 * @param session
	 * @return
	 */
	@RequestMapping("/bloodOxygen")
	@ResponseBody
	public List<BloodOxygen> getBloodOxygen(HttpServletRequest request){
		String personId = (String)request.getParameter("personId");
		BloodOxygen bOxygen = new BloodOxygen();
		if(personId!=null){
			bOxygen.setPersonId(personId);
		}
		return bOxygenServiceI.getBloodOxygen(bOxygen);
	}
	/**
	 * 保存或更新血氧信息
	 * @param bs
	 * @return
	 */
	@RequestMapping("/saveOrUpdate")
	@ResponseBody
	public Json saveOrUpdate(HttpServletRequest request) throws Exception{
		String json = request.getParameter("json");
		System.out.println("未转化数据：" + json);
//		json = URLDecoder.decode(json, "utf-8");
		String[] s = json.split("=");
		if (s.length > 1) {
			json = s[1];
		}
		System.out.println("已转化数据：" + json);
		BloodOxygen bo = new BloodOxygen();
		if (!json.isEmpty()) {
			bo = JSON.parseObject(json, BloodOxygen.class);
			bo.setId(null);
		}
		BloodOxygen bOxygen = bOxygenServiceI.saveOrUpdate(bo);
		Json j = new Json();
		if(bOxygen!=null&&!json.isEmpty()){
			j.setMsg("保存成功");
			j.setSuccess(true);
			j.setObj(bOxygen);
		}else{
			j.setMsg("保存失败");
			j.setSuccess(false);
		}
		return j;
	}
	
	@RequestMapping(value="/saveOrUpdate", method=RequestMethod.POST,consumes = "application/json")
	@ResponseBody
	public Json saveOrUpdate(@RequestBody BloodOxygen[] bos,HttpServletRequest request,HttpServletResponse response) throws Exception{
		System.out.println("未转化的数据："+JSON.toJSONString(bos));
		Json j = new Json();
		List<BloodOxygen> list = new ArrayList<BloodOxygen>();
		if(bos.length>0){
			for(BloodOxygen bo :bos){
				Long appLocalId = ValidatorUtil.isNullOnLong(bo.getId())?null:bo.getId();
				bo.setId(null);
				BloodOxygen bOxygen = bOxygenServiceI.saveOrUpdate(bo);
				if(bOxygen!=null){
					bo.setRespCode(0);//保存成功
				}else{
					bo.setRespCode(-1);//保存失败
				}
				bo.setAppLocalId(appLocalId);
				list.add(bOxygen);
			}
			j.setMsg("数据已经处理！");
			j.setSuccess(true);
			j.setObj(list);
		}else{
			j.setMsg("数据为空");
			j.setSuccess(false);
		}
		return j;
	}
	
	@RequestMapping("/getNewBloodOxygen")
	@ResponseBody
	public Json getNewBloodOxygen(BloodOxygen bo){
		Json j = new Json();
		BloodOxygen bOxygen = bOxygenServiceI.getNewBloodOxygen(bo);
		if(!ValidatorUtil.isNullOnLong(bOxygen.getId())){
			j.setMsg("获取成功");
			j.setSuccess(true);
			j.setObj(bOxygen);
		}else{
			j.setMsg("获取数据为空");
			j.setSuccess(false);
		}
		return j;
	}
	
}
